﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace Vehicle.Web.Pages.Task
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Task_Edit.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-09-02   Created by 杨光远 V1.00.0000
    /// 功能说明：修改任务单页面类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Task_Edit : Component.PageBase
    {
        #region ---基本变量实例化---
        Vehicle.BLL.Task b_Task = new Vehicle.BLL.Task();//定义逻辑层变量
        BLL.Log b_Log = new Vehicle.BLL.Log();
        Vehicle.Model.TaskInfo m_TaskInfo = new Vehicle.Model.TaskInfo();//定义实体层
        Vehicle.BLL.Vehicles b_Vehicles = new Vehicle.BLL.Vehicles();//定义逻辑层变量
        Vehicle.BLL.Motorcade b_Motorcade = new Vehicle.BLL.Motorcade();//定义逻辑层变量
        public string url;
        #endregion

        #region---基本操作---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                
                TaskDataBind();
                
            }
        }
         /// <summary>
        /// 初始绑定车队下拉数据
        /// </summary>
        public void MotorcadeDataBind()
        {
            b_Motorcade.DropDownListBind(drop_M_ID);//绑定车队
            drop_M_ID.Items.Remove(new ListItem("无", "0"));
            
            PilotDataBind();
        
        }
         /// <summary>
        /// 根据车队下拉菜单绑定下属车辆
        /// </summary>
        public void VehiclesDropDataBind()
        {
            b_Vehicles.DropDownListBind(drop_V_ID, new Vehicle.Model.ComponentsInfo("Vehicles", "M_ID", drop_M_ID.SelectedValue, "0", "0", "V_IsDelete", "1", "V_ID"));
            drop_V_ID.Items.Remove(new ListItem("无", "0"));
            PilotDataBind();
        }

        /// <summary>
        /// 初始绑定驾驶员
        /// </summary>
        public void PilotDataBind()
        {
            txt_P_ID.Text = null;
            try
            {
                lab_P_ID.Visible = false;
                DataTable dt = b_Task.GetModel(new Model.ComponentsInfo("Pilot", "V_ID", drop_V_ID.SelectedValue, "1", "1", "P_IsDelete", "1", "P_ID"));
                txt_P_ID.Text = dt.Rows[0]["P_Name"].ToString();//驾驶员姓名
                hf_P_ID.Value = dt.Rows[0]["P_ID"].ToString();//驾驶员ID
            }
            catch
            {
                lab_P_ID.Visible = true;
            }

        }

        /// <summary>
        /// 绑定任务方法
        /// </summary>
        public void TaskDataBind()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
               
                DataTable dt = b_Task.GetModel(new Vehicle.Model.ComponentsInfo("Task", "T_ID", Request.QueryString["id"].ToString(), "T_ID"));
                if (dt.Rows.Count > 0)
                {
                    txt_C_Card.Text=dt.Rows[0]["C_Card"].ToString();//绑定任务编号
                    MotorcadeDataBind();
                    drop_M_ID.SelectedValue=dt.Rows[0]["M_ID"].ToString();//绑定车队名称
                    VehiclesDropDataBind();//绑定车辆
                    drop_V_ID.SelectedValue=dt.Rows[0]["V_ID"].ToString();//绑定车辆
                     PilotDataBind();//绑定驾驶员
                    txt_T_Time.Text=dt.Rows[0]["T_Time"].ToString();//绑定任务的时间
                    txt_V_Note.Text=dt.Rows[0]["T_Note"].ToString();//绑定备注
                    Gmdp_T_StarTime.Date = Common.TextHelper.StringToDateTime(dt.Rows[0]["T_StarTime"].ToString());//具体发车时间
                    Gmdp_T_EndTime.Date = Common.TextHelper.StringToDateTime(dt.Rows[0]["T_EndTime"].ToString());//具体到达时间
                    drop_T_Type.SelectedValue = dt.Rows[0]["T_Type"].ToString();//状态 1执行中 2已完成 	0已取消  
                    hf_T_LastTime.Value=dt.Rows[0]["T_LastTime"].ToString();//最后更新时间
                    hf_T_Num.Value = dt.Rows[0]["T_Num"].ToString();//任务单编号
                    txt_C_Num.Text = dt.Rows[0]["T_Num"].ToString();//任务单编号
                    url = b_Task.GetModel(new Vehicle.Model.ComponentsInfo("Cargo", "C_Card", txt_C_Card.Text, "1", "1", "C_IsDelete", "1", "C_ID")).Rows[0]["C_ID"].ToString();//承运单ID
                    if (dt.Rows[0]["T_Type"].ToString() != "1")
                    {
                        drop_T_Type.Enabled = false;
                    }
                }
                else
                {
                    Common.MessageHelper.AlertBack("该记录已删除!");
                }
            }
            else
            {
                Common.MessageHelper.AlertBack("您的操作有误,请重新尝试!");
            }
        }
        /// <summary>
        /// 修改数据逻辑判断是否有数据更新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
                if (hf_Set.Value == "1")//取值判断
                {
                    //提交前判断数据是否有被改变
                    if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Task", "T_ID", Request.QueryString["id"].ToString(), "T_ID")).Rows[0]["T_LastTime"].ToString() == hf_T_LastTime.Value)
                    {
                        Repeat();
                    }
                    else
                    {
                        d_Show.Visible = true;
                        hf_Set.Value = "0";
                    }
                }
                else
                {
                    Repeat();
                }
            }
            else
            {
                Common.MessageHelper.AlertBack("您的操作有误,请重新尝试!");
            }
        }
        /// <summary>
        /// 逻辑判断是否有编号重复
        /// </summary>
        public void Repeat()
        {
            //判断当前输入的编号是否和之前的编号相同,不相同则先判断是否有重复的编号
            if (txt_C_Num.Text.Trim() != hf_T_Num.Value.ToString())
            {
                //添加前判断是否已经有该编号的任务
                if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Task", "T_Num", txt_C_Num.Text.Trim(), "T_ID")).Rows.Count == 0)
                {
                    UpdateDate();
                }
                else
                {
                    Common.MessageHelper.Alert("已经有相同该编号的任务单,请重新填写");
                }
            }
            else
            {
                UpdateDate();
            }
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        protected void UpdateDate()
        {
            m_TaskInfo.T_ID = Common.TextHelper.StringToInt(Request.QueryString["id"].ToString());
            m_TaskInfo.C_Card = txt_C_Card.Text.Trim();//承运单编号
            m_TaskInfo.M_ID = Common.TextHelper.StringToInt(drop_M_ID.SelectedValue);//承运车队ID
            m_TaskInfo.V_ID = Common.TextHelper.StringToInt(drop_V_ID.SelectedValue);//承运车辆ID
            m_TaskInfo.P_ID = Common.TextHelper.StringToInt(hf_P_ID.Value);//驾驶员ID
            m_TaskInfo.T_Time = Common.TextHelper.StringToDateTime(txt_T_Time.Text.Trim());//添加时间默认当前时间
            m_TaskInfo.T_Note = Common.TextHelper.CheckStr(txt_V_Note.Text);//任务备注
            m_TaskInfo.T_StarTime = Common.TextHelper.StringToDateTime(Gmdp_T_StarTime.DateString);//发车时间
            m_TaskInfo.T_EndTime = Common.TextHelper.StringToDateTime(Gmdp_T_EndTime.DateString);//结束时间
            m_TaskInfo.T_IsDelete = 0;//是否删除 默认0不删除
            m_TaskInfo.T_Type =Common.TextHelper.StringToInt( drop_T_Type.SelectedValue);//任务状态 1执行中 2 已完成
            m_TaskInfo.T_LastTime = DateTime.Now;//最后更新时间
            m_TaskInfo.T_Num = txt_C_Num.Text.Trim();//任务编号
            b_Task.Update(m_TaskInfo);
            if (drop_T_Type.SelectedValue != "1")//任务状态不为执行中
            {
                b_Task.AllUpdate(new Model.ComponentsInfo("Pilot", "P_Transit", "0", "P_ID", hf_P_ID.Value));//更新驾驶员
                b_Task.AllUpdate(new Model.ComponentsInfo("Vehicles", "V_Transit", "0", "V_ID", drop_V_ID.SelectedValue));//更新车辆
            }
            b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功修改任务编号为" + txt_C_Num.Text.Trim()+ "的任务信息"));//添加日志
            Common.MessageHelper.AlertRedirect("任务修改成功!", "../Cargo/Main.aspx");
        }
        /// <summary>
        /// 绑定车队
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_M_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            VehiclesDropDataBind();
        }
        /// <summary>
        /// 绑定驾驶员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_P_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            PilotDataBind();
        }
        #endregion
    }
}
